package com.zkca.xf.presenter;

import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.zkca.xf.base.BasePresenter;
import com.zkca.xf.manager.DataManager;
import com.zkca.xf.model.javabean.SmsBean;
import com.zkca.xf.model.javabean.UserBean_W;
import com.zkca.xf.model.javabean.UserId;
import com.zkca.xf.model.javabean.uploadbean.RequestSms;
import com.zkca.xf.model.javabean.uploadbean.RequestUser;
import com.zkca.xf.view.LoginCodeView;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by li on 2018/10/9.
 */

public class LoginCodePresenter extends BasePresenter {
    private DataManager dataManager;
    private LoginCodeView loginCodeView;

    public LoginCodePresenter(LifecycleProvider<ActivityEvent> provider, LoginCodeView loginCodeView) {
        super(provider);
        this.loginCodeView = loginCodeView;
        dataManager = DataManager.getInstance();
    }

    //获取短信验证码
    public void getSMS(RequestSms requestSms) {
        dataManager.getSms(requestSms)
                .subscribeOn(Schedulers.io())// 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread())// UI线程处理返回接口
                .compose(getProvider().<SmsBean>bindUntilEvent(ActivityEvent.DESTROY))// onDestroy取消订阅
                .subscribe(new DefaultObserver<SmsBean>() {// 订阅
                    @Override
                    public void onNext(SmsBean s) {
                        loginCodeView.getSms(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        loginCodeView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        loginCodeView.hideProgressDialog();
                    }
                });

    }

    //短信验证码登录
    public void logInByCode(RequestUser requestUser) {
        dataManager.loginByCode(requestUser)
                .subscribeOn(Schedulers.io())// 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread())// UI线程处理返回接口
                .compose(getProvider().<UserBean_W>bindUntilEvent(ActivityEvent.DESTROY))// onDestroy取消订阅
                .subscribe(new DefaultObserver<UserBean_W>() {// 订阅
                    @Override
                    public void onNext(UserBean_W s) {
                        loginCodeView.getUser(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        loginCodeView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        loginCodeView.hideProgressDialog();
                    }
                });

    }
}
